package manager.auxiliares;

import com.lowagie.text.Font;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import manager.principal.fonte.JanelaLogin;



public class ConnectionStatus implements ActionListener {

   private javax.swing.Timer timer;
   JLabel labelTime;
   JLabel labelIcon;
   JLabel labelStatus;
   JLabel mensagem;
   private JFrame frame;
   boolean ativoDesativo = false;
   boolean conexao = true;
   public ConnectionStatus(JLabel labelTime,JLabel labelIcon, JLabel labelStatus){
      disparaRelogio();
      this.labelTime = labelTime;
      this.labelIcon = labelIcon;
      this.labelStatus = labelStatus;
   }

   public void disparaRelogio() {
      if (timer == null) {
         timer = new javax.swing.Timer(1000, this);
         timer.setInitialDelay(0);
         timer.start();
      } else if (!timer.isRunning()) {
         timer.restart();
      }
   }
    public void montaTela(){
      
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      mensagem.setFont( new java.awt.Font("Itálico", Font.ITALIC, 15));
      JPanel panel = new JPanel();
      panel.add(mensagem);
      panel.setLayout(new FlowLayout(FlowLayout.CENTER));

      frame.getContentPane().add(panel);

      frame.setResizable(false);
      frame.setBounds(FlowLayout.CENTER, FlowLayout.CENTER, 500, 200);
      panel.setBackground(new Color(255, 255, 255));
      frame.setLocationRelativeTo(null);
      frame.setUndecorated(true);
    }
   public void actionPerformed(ActionEvent ae) {
     try{
        GregorianCalendar calendario = new GregorianCalendar();
        int h = calendario.get(GregorianCalendar.HOUR_OF_DAY);
        int m = calendario.get(GregorianCalendar.MINUTE);
        int s = calendario.get(GregorianCalendar.SECOND);

        String hora =
         ((h < 10) ? "0" : "")
            + h
            + ":"
            + ((m < 10) ? "0" : "")
            + m
            + ":"
            + ((s < 10) ? "0" : "")
            + s;
          labelTime.setText(hora);

        boolean validarConexao = JanelaLogin.getConnection().isValid(2);
        if(validarConexao){
            if(ativoDesativo == false ) {
                labelStatus.setText("Conectado");
                labelStatus.setForeground(new Color(0, 153, 0));
                labelIcon.setIcon(new Icons().getConected());
                ativoDesativo = true;
                labelStatus.repaint();
                labelIcon.repaint();
            }
        }
        else{
            if(ativoDesativo == true ) {
                labelStatus.setText("Desconectado");
                labelStatus.setForeground(new Color(204,0,51));
                labelIcon.setIcon(new Icons().getDesconected());
                ativoDesativo = false;
                labelStatus.repaint();
                labelIcon.repaint();
                if(conexao){
                    JOptionPane.showMessageDialog(null, "O software perdeu a comunicação com o banco de dados devido a problemas na rede. É aconselhavel que se feche e abra o programa novamente para evitar erros.", "Problema de Conexão", JOptionPane.WARNING_MESSAGE);
                    conexao = false;
                }
            }            
            JanelaLogin.newConnection();
        }

            
     }
    catch(Exception e){
        e.printStackTrace();
    }
   }
}